package com.example.encrypt;

import android.os.Build;

import java.util.Base64;

import androidx.annotation.RequiresApi;

public class Base64s {

    /**
     * Base64
     *
     */
    public static void base64(String str) {
        byte[] bytes = str.getBytes();


    }

    /**
     * BASE64加密解密
     */
    public static void enAndDeCode(String str) throws Exception {
        String data = encryptBASE64(str.getBytes());
        System.out.println("sun.misc.BASE64 加密后：" + data);

        byte[] byteArray = decryptBASE64(data);
        System.out.println("sun.misc.BASE64 解密后：" + new String(byteArray));
    }

    /**
     * BASE64解密
     * @throws Exception
     */
    public static byte[] decryptBASE64(String key) throws Exception {
        return (Base64.getDecoder()).decode(key);
    }

    /**
     * BASE64加密
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String encryptBASE64(byte[] key) throws Exception {
        return (Base64.getEncoder()).encodeToString(key);
    }

}
